package com.wms.ymcc.web.controller;

import com.wms.ymcc.dto.AlipayNotifyDto;
import com.wms.ymcc.dto.PayApplyDto;
import com.wms.ymcc.result.JSONResult;
import com.wms.ymcc.service.IPayOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author : 阿盛哟
 * @description : 订单支付控制类
 * @createDate : 2025/6/23 15:30
 */
@Slf4j
@RestController
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private IPayOrderService payOrderService;




    /**
     * 支付宝异步回调的接口
     * dto:去接收支付宝的参数
     * AlipayNotifyDto alipayNotifyDto: 原始方式发送的参数，不是通过 json 发送的参数
     */
    @PostMapping("/notify")
    public String payNotify(AlipayNotifyDto alipayNotifyDto) {
        try {
            return payOrderService.payNotify(alipayNotifyDto);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 检查支付单是否已生成
     * @param orderNo
     * @return
     */
    @GetMapping("/checkPayOrder/{orderNo}")
    public JSONResult checkPayOrder(@PathVariable("orderNo") String orderNo) {
        log.info("检查支付单是否已生成");
        return payOrderService.checkPayOrder(orderNo);
    }

    /**
     * 支付请求
     */
    @PostMapping("/apply")
    public JSONResult apply(@RequestBody PayApplyDto payApplyDto) {

        String payForm = payOrderService.apply(payApplyDto);

        return JSONResult.success(payForm);
    }

}